import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JButton;
import javax.swing.JPanel;


public class OpenScreen extends JPanel implements ActionListener {

	private Clip bgm;
	
	private Image title, gameStart, end = null;
	
	Rectangle rect, rect2;
	
	public OpenScreen(){
		try{
		URL	url = getClass().getResource("opening.wav");
		AudioInputStream openbgm = 
				AudioSystem.getAudioInputStream(url);//오디오파일에 대한 스트림
		bgm = AudioSystem.getClip();
		bgm.open(openbgm);
		bgm.loop(Clip.LOOP_CONTINUOUSLY);
		}catch(Exception ex){ex.printStackTrace();}
		bgm.start();
		init();
	}
	
	
	public void paint(Graphics g) {
	
		super.paint(g);
		g.drawImage(title, 0, 0, null);
		g.drawImage(gameStart,  rect.x,  rect.y, null);
		g.drawImage(end,  rect2.x, rect2.y , null);
	}
	private void init() {
		setLayout(null);
		setSize(Constants.SCREEN_WIDTH,Constants.SCREEN_HEIGHT);
		try {
			URL ur1 = getClass().getResource("title.png");
			Image image = ImageIO.read(ur1);
			title = image.getScaledInstance(Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT,
					Image.SCALE_SMOOTH);
			
			
			ur1 = getClass().getResource("gameStart.png");
//			  image = ImageIO.read(ur1);
//			gameStart = image.getScaledInstance(Constants.SCREEN_WIDTH/4, Constants.SCREEN_HEIGHT/4,
//					Image.SCALE_SMOOTH);
			  gameStart = ImageIO.read(ur1); 
			rect =new Rectangle(gameStart.getWidth(null), gameStart.getHeight(null));

			
			ur1 = getClass().getResource("exit.png");
//			  image = ImageIO.read(ur1);
//			end = image.getScaledInstance(Constants.SCREEN_WIDTH/4, Constants.SCREEN_HEIGHT/4,
//					Image.SCALE_SMOOTH);
			end = ImageIO.read(ur1);
		
			rect2 =new Rectangle(end.getWidth(null), end.getHeight(null));

			
		
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
		rect.x = (Constants.SCREEN_WIDTH-gameStart.getWidth(null))/2;//위치정보
		rect.y = (Constants.SCREEN_HEIGHT+gameStart.getHeight(null))/2;
		
		rect2.x = (Constants.SCREEN_WIDTH-end.getWidth(null))/2;//위치정보
		rect2.y = rect.y+gameStart.getHeight(null)+end.getHeight(null)/2;
		
		JButton newGame = new JButton("GAME START");
		newGame.setSize(387,71);
		newGame.setLocation(rect.x,
				rect.y);
		newGame.addActionListener(this);
		add(newGame);
		
		JButton gameExit = new JButton("EXIT");
		gameExit.setSize(126, 68);
		gameExit.setLocation(rect2.x,
				rect2.y);
		gameExit.addActionListener(this);
		add(gameExit);
//		setBackground(Color.white);
		validate();
		repaint();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		JButton clickedButton = (JButton)e.getSource();
		if (clickedButton.getText().equals("GAME START")) {
			bgm.stop();
			validate();
			MainScreen screen = new MainScreen();
			screen.setFocusable(true);//컴포넌트가 이벤트를 수신할 수 있도록 설정(명시적으로 너는 이벤트를 받는애다!)
			screen.setSize(Constants.SCREEN_WIDTH,Constants.SCREEN_HEIGHT);
			Container frame = getParent();
			frame.add(screen);
			frame.remove(this);
			frame.validate();
			frame.repaint();
		}else if(clickedButton.getText().equals("EXIT")){
				System.exit(0);
		}
		
	}
}
